/**
 * Project Name:cnsy-url-short-service
 * File Name:FramePageController.java
 * Package Name:cn.cnsy123.shorturls.api.controller.page 
 * Date: 2020年4月14日-下午1:08:44
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.api.controller.page;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.cnsy123.shorturls.api.controller.result.SysMenuResult;
import cn.cnsy123.shorturls.api.controller.vo.TreeMenuVo;
import cn.cnsy123.shorturls.business.service.SysMenuService;
import cn.cnsy123.shorturls.business.util.TreeMenuUtil;
import cn.cnsy123.shorturls.cache.PageAddrCache;
import cn.cnsy123.shorturls.constants.SessionInfoConstants;

/**
 * ClassName: FramePageController
 * 
 * @author yangpengfei
 * @date 2020年4月14日-下午1:08:44
 * @version 1.0
 * @since JDK 1.7
 */
@Controller
@RequestMapping(value = "/page")
public class FramePageController {
    
    @Autowired
    private SysMenuService sysMenuService;
    
    
    @RequestMapping(value = "/{detail}",method = RequestMethod.POST,consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ModelAndView skipMainFramePage(@PathVariable(value = "detail") String viewName) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        boolean flag = PageAddrCache.checkUrl(viewName);
        if(flag) {
            modelAndView.setViewName(viewName);
        }else {
            modelAndView.setViewName("error/404_frame");
        }
        return modelAndView;
    }
    
    @RequestMapping(value = "/{detail}",method = RequestMethod.GET)
    public ModelAndView skipMainFramePage(@PathVariable(value = "detail") String viewName,HttpServletRequest request) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        String requestUrl = request.getServletPath();
        Object attribute = request.getSession().getAttribute(SessionInfoConstants.SESSION_USER_BEAN);
        if(attribute != null) {
            List<TreeMenuVo> treeMenus = sysMenuService.selectTreeMenuByUserId(1L);
            if(!CollectionUtils.isEmpty(treeMenus)) {
                Map<String, SysMenuResult> menuUrls = TreeMenuUtil.convertDataByMenuUrl(treeMenus);
                if(menuUrls != null && menuUrls.containsKey(requestUrl)) {
                    request.getSession().setAttribute(SessionInfoConstants.SESSION_VISIT_NODE, menuUrls.get(requestUrl).getId());
                    request.getSession().setAttribute(SessionInfoConstants.SESSION_VISIT_NODE_ADDR, requestUrl);
                    modelAndView.setViewName("mainPage");
                }else {
                    modelAndView.setViewName("error/404");
                }
            }else {
                modelAndView.setViewName("error/500");
            }
        }else {
            modelAndView.setViewName("redirect:/loginPage?error="+URLEncoder.encode("登录超时，请重新登录！", "UTF-8"));
        }
        return modelAndView;
    }
    
    

}
